home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 13416 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  945 b 

  1. Path: ccshst05.cs.uoguelph.ca!ccshst01!thay
  2. From: thay@uoguelph.ca (Toby K Hay)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: I need random number function that returns a number between x and i ?
  5. Date: 6 Apr 1996 22:01:51 GMT
  6. Organization: University of Guelph
  7. Message-ID: <4k6pkf$3qj@ccshst05.cs.uoguelph.ca>
  8. References: <3166C942.2D8@ccis.com>
  9. NNTP-Posting-Host: ccshst01.cs.uoguelph.ca
  10. X-Newsreader: TIN [version 1.2 PL2]
  11.  
  12. Bill Lund (wlund@ccis.com) wrote:
  13. : I need random number function that returns a number between x and i ?
  14. : Something like :
  15. : int randomer(int x , int i).
  16. : And returns a random number  between x and i.
  17.  
  18. Try:
  19. int Randomer(int x, int i)
  20. {
  21.     return (x + rand() / ((RAND_MAX / (1 + i - x) + 1));
  22. }
  23.  
  24. It should return a random integer in the interval [x, i] unless I've made 
  25. a silly mistake.  You might want to include a test of whether i is in 
  26. fact greater than x to avoid errors such as divide by 0.
  27. Toby Hay    thay@uoguelph.ca
  28.